package com.xuecheng.content.api;

import com.xuecheng.base.execption.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.*;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseCategoryService;
import com.xuecheng.content.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Api(value = "课程信息管理接口",tags = "课程信息管理接口")
@RestController
@Slf4j
public class CourseBaseInfoController {


    @Autowired
    private CourseBaseService courseBaseService;


    @ApiOperation("课程查询接口")
    @PostMapping("/course/list")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required=false) QueryCourseParamsDto queryCourseParamsDto) {
        log.info("查询课程");
        //当前登录用户
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //用户所属机构id
        Long companyId = null;
        if(StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }
        PageResult<CourseBase> pageResult = courseBaseService.pageList(pageParams,queryCourseParamsDto,companyId);
        return pageResult;
    }

    @ApiOperation("课程添加接口")
    @PostMapping("/course")
    public CourseBaseInfoDto saveCourseBase(@RequestBody @Validated({ValidationGroups.Inster.class})AddCourseDto addCourseDto){
        //当前登录用户
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //用户所属机构id
        Long companyId = null;
        if(StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }
        Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();

        CourseBaseInfoDto courseBaseInfoDto = courseBaseService.saveCourseBase(addCourseDto,companyId);
        return courseBaseInfoDto;
    }

    @ApiOperation("课程删除接口")
    @DeleteMapping("/course/{courseId}")
    public void deleteCourse(@PathVariable Long courseId){
        courseBaseService.deleteCourse(courseId);
    }

    @ApiOperation("根据课程id查课程接口")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto selectCourseById(@PathVariable Long courseId){
        return courseBaseService.selectCourseById(courseId);
    }

    @ApiOperation("课程更新接口")
    @PutMapping("/course")
    public CourseBaseInfoDto  updateCourse(@RequestBody @Validated({ValidationGroups.Update.class})UpdateCourseDto dto){
        //当前登录用户
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //用户所属机构id
        Long companyId = null;
        if(StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }
        return courseBaseService.UpdateCourse(dto,companyId);
    }

    @GetMapping("/r")
    public String r(){
        return "rrrr";
    }
}
